//
//  HWRechargeView.m
//  嗨文
//
//  Created by 响当当 on 2019/1/14.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWRechargeView.h"
#import "HWRechargeButton.h"
#import "HWStraipManager.h"

@interface HWRechargeView()
@property (nonatomic, strong) UILabel *balanceLabel;//余额
@property (nonatomic, strong) UIButton *weixinPayButton;//微信支付
@property (nonatomic, strong) UIButton *aPayButton;//支付宝支付
@property (nonatomic, strong) UIButton *rechargeMoneyButton;//开通

@property (nonatomic, strong) NSMutableArray *rechargeArr;//存充值按钮
@property (nonatomic, strong)HWRechargeModel *rechargeModel;//
@property (nonatomic, assign)NSInteger rechargeNum;//
@property (nonatomic, strong)NSString *rechargeId;//
@property (nonatomic, strong)HWStraipManager *IAPManager;

@property (nonatomic, strong)NSString *hide;//

@end

@implementation HWRechargeView

- (NSMutableArray *)rechargeArr
{
    if (!_rechargeArr)
    {
        _rechargeArr = [[NSMutableArray alloc]init];
    }
    return _rechargeArr;
}

- (UIButton *)weixinPayButton
{
    if (!_weixinPayButton)
    {
        _weixinPayButton = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.aPayButton.frame)+15*kUIScaleBasePlus,363*kUIScaleBasePlus,90*kUIScaleBasePlus,58*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentCenter
                                                       text:@"微信"
                                                   textClor:LCHexColor(0x47C961)
                                                  buttonBGC:[UIColor clearColor]
                                                   textFont:10*kUIScaleBasePlus
                                                     target:self
                                                     method:@selector(weixinPay:)];
        [_weixinPayButton setImage:[UIImage imageNamed:@"微信"] forState:UIControlStateNormal];
        [_weixinPayButton setUpImageAndDownLableWithSpace:1];
        _weixinPayButton.layer.masksToBounds = YES;
        _weixinPayButton.layer.cornerRadius = 4;
        _weixinPayButton.layer.borderWidth = 1;
        _weixinPayButton.layer.borderColor = LCHexColor(0x89898C).CGColor;
        _weixinPayButton.hidden = YES;
    }
    return _weixinPayButton;
}

- (UIButton *)aPayButton
{
    if (!_aPayButton)
    {
        _aPayButton = [HWUtilties creatButtonWithFrame:CGRectMake(15*kUIScaleBasePlus,363*kUIScaleBasePlus,90*kUIScaleBasePlus,58*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentCenter
                                                       text:@"支付宝"
                                                   textClor:LCHexColor(0x07A0F8)
                                                  buttonBGC:[UIColor clearColor]
                                                   textFont:12*kUIScaleBasePlus
                                                     target:self
                                                method:@selector(aPay:)];
        [_aPayButton setImage:[UIImage imageNamed:@"支付宝"] forState:UIControlStateNormal];
        [_aPayButton setUpImageAndDownLableWithSpace:1];
        _aPayButton.layer.masksToBounds = YES;
        _aPayButton.layer.cornerRadius = 4;
        _aPayButton.layer.borderWidth = 1;
        _aPayButton.layer.borderColor = LCHexColor(0x07A0F8).CGColor;
        _aPayButton.hidden = YES;
    }
    return _aPayButton;
}

- (UIButton *)rechargeMoneyButton
{
    if (!_rechargeMoneyButton)
    {
        _rechargeMoneyButton = [HWUtilties creatButtonWithFrame:CGRectMake(267*kUIScaleBasePlus,370*kUIScaleBasePlus,86*kUIScaleBasePlus,42*kUIScaleBasePlus)
                                                   textAligment:NSTextAlignmentCenter
                                                           text:@"立即开通"
                                                       textClor:LCHexColor(0xFFFFFF)
                                                      buttonBGC:LCHexColor(0xF54337)
                                                       textFont:16*kUIScaleBasePlus
                                                         target:self
                                                         method:@selector(openVip)];
        _rechargeMoneyButton.layer.masksToBounds = YES;
        _rechargeMoneyButton.layer.cornerRadius = 4;
    }
    return _rechargeMoneyButton;
}

- (UILabel *)balanceLabel
{
    if (!_balanceLabel)
    {
        _balanceLabel = [HWUtilties creatLabelWithFrame:CGRectMake(16*kUIScaleBasePlus,20*kUIScaleBasePlus, SCREEN_WIDTH-16*kUIScaleBasePlus,14*kUIScaleBasePlus)
                                           textAligment:NSTextAlignmentLeft
                                               textClor:LCHexColor(0x000000)
                                               textFont:15*kUIScaleBasePlus];
        
    }
    return _balanceLabel;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.rechargeNum = 12;
        self.rechargeId = @"1";
        [self addSubview:self.balanceLabel];
        [self addSubview:self.aPayButton];
        [self addSubview:self.weixinPayButton];
        [self addSubview:self.rechargeMoneyButton];
        [self attention];
    }
    return self;
}

#pragma mark - 充值金额按钮创建
- (void)creatUI:(HWRechargeModel *)model
{
    CGFloat width = 165.0*kUIScaleBasePlus;
    CGFloat height = 88.0*kUIScaleBasePlus;
    
    CGFloat margin = 15*kUIScaleBasePlus;
    
//    NSArray *arr = @[@{@"money":@"¥6",@"coin":@"600书币",@"book":@"60书券"},
//                     @{@"money":@"¥12",@"coin":@"1200书币",@"book":@"180书券"},
//                     @{@"money":@"¥30",@"coin":@"3000书币",@"book":@"600书券"},
//                     @{@"money":@"¥50",@"coin":@"5000书币",@"book":@"2500书券"},
//                     @{@"money":@"¥98",@"coin":@"9800书币",@"book":@"4900书券"},
//                     @{@"money":@"¥320",@"coin":@"32000书币",@"book":@"32000书券"},];
    
    for (int i=0; i<6;i++)
    {
        HWRechargeButton *button = [[HWRechargeButton alloc]initWithFrame:CGRectMake(margin+(i%2)*(margin+width),(i/2)*(margin+height)+CGRectGetMaxY(self.balanceLabel.frame)+17*kUIScaleBasePlus,width,height)];
        [button getRechargeDate:model.goodsList[i] andIsFirstRecharge:[model.isFirstRecharge stringValue] ishide:self.hide];
        if (i == 0 )
        {
            [button setBackgroundColor:LCHexColor(0xF54337)];
            [button btnSelectedAndChangeState];
        }
        button.tag = i;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.rechargeArr addObject:button];
        [self addSubview:button];
    }
}

#pragma mark - 温馨提示
- (void)attention
{
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(16*kUIScaleBasePlus,445*kUIScaleBasePlus,3*kUIScaleBasePlus,15*kUIScaleBasePlus)];
    lineView.backgroundColor = LCHexColor(0xF54337);
    [self addSubview:lineView];
    
    UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(lineView.frame)+10*kUIScaleBasePlus,445*kUIScaleBasePlus, 120*kUIScaleBasePlus,14*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentLeft
                                                 textClor:LCHexColor(0x333333)
                                                 textFont:15*kUIScaleBasePlus];
    titleLabel.text = @"温馨提示";
    [titleLabel setFont:[UIFont boldSystemFontOfSize:15*kUIScaleBasePlus]];
    [self addSubview:titleLabel];
    
    UILabel *contextLabel = [HWUtilties creatLabelWithFrame:CGRectMake(16*kUIScaleBasePlus,CGRectGetMaxY(titleLabel.frame)+19*kUIScaleBasePlus,344*kUIScaleBasePlus,91*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0x595656)
                                                   textFont:13*kUIScaleBasePlus];
    contextLabel.numberOfLines = 0;
    contextLabel.text = @"1.嗨小说充值赠送的书券，仅限嗨小说平台使用。且赠送的书券15天内有效，阅读时优先扣除赠送券\n2.书币充值比例为1元= 100书币\n3.购买后，账户余额若长时间无变化，请致电书城客服0512-00000000，或者联系客服QQ：000000000";
    [contextLabel sizeToFit];
    [self addSubview:contextLabel];
}

#pragma mark - 选中充值按钮状态展现
- (void)btnClick:(UIButton *)sender
{
    
    [self.rechargeArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HWRechargeButton *button = obj;
        if (idx == sender.tag)
        {
            [button setBackgroundColor:LCHexColor(0xF54337)];
            [button btnSelectedAndChangeState];
        }
        else
        {
            [button setBackgroundColor:[UIColor clearColor]];
            [button btnUnSelectedAndChangeState];
        }
    }];
    
    self.rechargeId = self.rechargeModel.goodsList[sender.tag][@"rechargeId"];
    
    switch (sender.tag)
    {
        case 0:
        {
            self.rechargeNum = 12;
        }
            break;
        case 1:
        {
            self.rechargeNum = 25;
        }
            break;
        case 2:
        {
            self.rechargeNum = 30;
        }
            break;
        case 3:
        {
            self.rechargeNum = 50;
        }
            break;
        case 4:
        {
            self.rechargeNum = 108;
        }
            break;
        case 5:
        {
            self.rechargeNum = 208;
        }
            break;
        default:
            break;
    }
}

#pragma mark - 微信支付
- (void)weixinPay:(UIButton *)sender
{
    
    self.aPayButton.layer.borderColor = LCHexColor(0x89898C).CGColor;
    self.weixinPayButton.layer.borderColor = LCHexColor(0x47C961).CGColor;

}

#pragma mark - 支付宝支付
- (void)aPay:(UIButton *)sender
{
    
    self.weixinPayButton.layer.borderColor = LCHexColor(0x89898C).CGColor;
    self.aPayButton.layer.borderColor = LCHexColor(0x07A0F8).CGColor;
    
}

#pragma mark - 充值
- (void)openVip
{
 
    if ([self.hide isEqualToString:@"1"])
    {
        [self purchaseAction];
    }
    else
    {
        NSArray *arr = [[NSArray alloc]init];
        
        arr = self.rechargeModel.goodsList;
        
        NSDictionary *dic = arr[self.rechargeNum];
        
        NSNumber *num = IFNULL(dic[@"rechargeId"],[NSNumber numberWithInt:0]);
        
        NSDictionary *prarmers = @{
                                   @"access_token" : current_User.token,
                                   @"userId" : current_User.userID,
                                   @"rechargeId" : [num stringValue]
                                   };
        
        [NetWorkingManager postRequestUrl:FULL_WEB_URL(kAPI_AIPAYVIP) withPramater:prarmers sucess:^(id responseObject) {
            [HWUtilties openUrlSchedule:responseObject[@"data"]];
        } failure:^(NSError *error) {
            
        }];

    }
}

- (void)purchaseAction
{
    [SVProgressHUD showWithStatus:@"加载中"];
    
    if (!_IAPManager)
    {
        _IAPManager = [HWStraipManager shareSIAPManager];
    }
    // iTunesConnect 苹果后台配置的产品ID
    
    WeakSelf(self)
    [_IAPManager startPurchWithID:[NSString stringWithFormat:@"%@.%ld",@"sb",self.rechargeNum]
                       rechargeId:self.rechargeId
                          payType:@"1"
                   completeHandle:^(SIAPPurchType type, NSData *data) {
                       [SVProgressHUD dismiss];
                       if (type == SIAPPurchVerSuccess)
                       {
                           if (weakSelf.paySuccessBlock)
                           {
                               weakSelf.paySuccessBlock();
                           }
                       }
                   }];
    
}

#pragma mark - 获取数据
- (void)getInfo:(HWRechargeModel *)model
{
    self.balanceLabel.text = [NSString stringWithFormat:@"书币：%@ + %@书券",[model.bookCoin stringValue],[model.bookCoupon stringValue]];
    if (self.rechargeArr.count == 0)
    {
        [self creatUI:model];
    }
    self.rechargeModel = model;

}

- (void)hideWexiOrAliPay:(NSString *)hide
{
    self.hide = hide;
    if ([hide isEqualToString:@"1"])
    {
        self.aPayButton.hidden = YES;
//        self.weixinPayButton.hidden = YES;
    }
    else
    {
        self.aPayButton.hidden = NO;
//        self.weixinPayButton.hidden = NO;
    }
}
@end
